package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.HomeRecommandAdapter;
import com.weixin.paleituo.annotation.SuccessFromType;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.HomeRecommandBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.RecommandDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 支付成功/收货成功界面
 * 根据type判断具体的页面逻辑，更改图片以及文字，
 *
 * @author fengzhen
 * @version v1.0, 2018/4/8
 */
public class PaySuccessActivity extends BasePersonActivity {

    @BindView(R.id.img_activity_pay_success)
    ImageView mImgActivityPaySuccess;
    @BindView(R.id.tv_info_activity_pay_success)
    TextView mTvInfoActivityPaySuccess;
    @BindView(R.id.recycle_activity_pay_success)
    RecyclerView mRecycleActivityPaySuccess;

    private int mType;

    /**
     * 跳转成功展示，推荐商品界面
     *
     * @param type {@link SuccessFromType} 成功类型
     * @return
     */
    public static Intent call2SuccessActivity(Context context, @SuccessFromType int type) {
        Intent intent = new Intent(context, PaySuccessActivity.class);
        intent.putExtra(Contans.INTENT_TYPE, type);
        return intent;
    }

    @Override
    protected View getContentView(Context context) {
        return UIUtils.inflate(context, R.layout.activity_pay_success);
    }

    @Override
    protected void initView() {
        super.initView();
        mType = getIntent().getIntExtra(Contans.INTENT_TYPE, -1);
    }

    @Override
    protected void initData() {
        super.initData();
        switch (mType) {
            case SuccessFromType.PAY:
                mTvTitle.setText("支付成功");
                mTvInfoActivityPaySuccess.setText("支付成功");
                mImgActivityPaySuccess.setImageResource(R.mipmap.icon_payment_success);
                break;
            case SuccessFromType.GETGOODS:
                mTvTitle.setText("确认收货成功");
                mTvInfoActivityPaySuccess.setText("确认收货成功！");
                mImgActivityPaySuccess.setImageResource(R.mipmap.icon_payment_success);
                break;
        }

        getRecommendList();
    }

    /**
     * 重新加载
     */
    private void getRecommendList() {
        Observable<StatusCode<List<HomeRecommandBean>>> observable = ApiUtils.getApi().getHomeRecommand(BaseApplication.getUserId(), BaseApplication.getToken(), Contans.rows, 1);
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<List<HomeRecommandBean>>(this) {
                    @Override
                    protected void _onNext(StatusCode<List<HomeRecommandBean>> statusCode) {
                        List<HomeRecommandBean> homeRecommandBeansData = statusCode.getData();
                        HomeRecommandAdapter homeRecommandAdapter = new HomeRecommandAdapter(PaySuccessActivity.this, homeRecommandBeansData);
                        mRecycleActivityPaySuccess.setItemAnimator(new DefaultItemAnimator());
                        mRecycleActivityPaySuccess.setLayoutManager(new GridLayoutManager(PaySuccessActivity.this, 2));
                        mRecycleActivityPaySuccess.addItemDecoration(new RecommandDividerItemDecoration(PaySuccessActivity.this));
                        mRecycleActivityPaySuccess.setAdapter(homeRecommandAdapter);
                    }

                    @Override
                    protected void _onError(String message) {
                    }
                }, "", lifecycleSubject, false, true);
    }

    @Override
    protected void returnMainActivity(View v) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        switch (mType) {
            case SuccessFromType.PAY:
                MyOrderActivity.startActivity(this, BaseApplication.getUserId(), 2);
                break;
            case SuccessFromType.GETGOODS:
                ToastUtils.makeText("未开发，跳转到完成订单列表");
                break;
        }
        finish();
    }
}
